package com.cn.admin.service.impl;

import com.cn.admin.dao.UserMapper;
import com.cn.admin.entity.Role;
import com.cn.admin.entity.User;
import com.cn.admin.entity.UserDetail;
import com.cn.admin.exception.CustomException;
import com.cn.admin.vo.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("CustomUserDetailsService")
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private  UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new CustomException(ResponseObject.fail(10001,"用户不存在"));
        }
        Role role=new Role();
        role.setName("admin");
        UserDetail userDetail=new UserDetail(user.getId(),user.getUsername(),user.getPassword());
        userDetail.setRole(role);
        return userDetail;
    }
}
